/**
 * Created by axnshy on 16/5/20.
 */
package com.ken.android.music.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.net.Uri;
import android.provider.MediaStore;

import com.ken.android.music.model.MusicBean;

import java.util.ArrayList;
import java.util.List;


public class MusicDao {

    private static final String TABLE_MUSIC = "music_info";
    private static final String TABLE_FAVORITE = "favorite_info";
    private static final String TABLE_EAST = "east_info";
    private static final String TABLE_ANIME = "anime_info";

    private static Object lock = new Object();
    private static volatile MusicDao mDao;

    public static MusicDao get() {
        if (mDao == null)
            synchronized (lock) {
                if (mDao == null) {
                    mDao = new MusicDao();
                }
            }
        return mDao;
    }

    public void saveMusicInfo(List<MusicBean> list, Context context) {
        SQLiteDatabase db = DatabaseHelper.getInstance(context);
        for (MusicBean music : list) {
            ContentValues cv = new ContentValues();
            cv.put(MusicBean.KEY_ID, music.musicId);
            cv.put(MusicBean.KEY_ALBUM, music.album);
            cv.put(MusicBean.KEY_DURATION, music.duration);
            cv.put(MusicBean.KEY_MUSIC_NAME, music.musicName);
            cv.put(MusicBean.KEY_ARTIST, music.artist);
            cv.put(MusicBean.KEY_DATA, music.data);
            cv.put(MusicBean.KEY_SIZE, music.size);

            db.insert(TABLE_MUSIC, null, cv);
        }
    }


/*    private List<MusicInfo> parseCursor(Cursor cursor) {
        List<MusicInfo> list = new ArrayList<MusicInfo>();
        while (cursor.moveToNext()) {
            MusicInfo music = new MusicInfo();
            music.musicId = cursor.getInt(cursor.getColumnIndex("_id"));
            music.album = cursor.getString(cursor.getColumnIndex("album"));
            music.duration = cursor.getInt(cursor.getColumnIndex("duration"));
            music.musicName = cursor.getString(cursor.getColumnIndex("musicname"));
            music.artist = cursor.getString(cursor.getColumnIndex("artist"));
            music.data = cursor.getString(cursor.getColumnIndex("data"));
            music.size = cursor.getInt(cursor.getColumnIndex("size"));
            music.displayName = cursor.getString(cursor.getColumnIndex("displayName"));
            list.add(music);
        }
        cursor.close();
        return list;
    }*/

    public void addToList(SQLiteDatabase database, ArrayList<MusicBean> list, String ListName) {
        for (MusicBean music : list) {
            int musicId = music.musicId;
            database.execSQL("insert into " + ListName + " (musicID) values " + musicId);
        }
    }

    public ArrayList<MusicBean> getListDet(Context context, String listTableName) {
        ArrayList<MusicBean> musicList = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = DatabaseHelper.getInstance(context);
        Cursor cursor = sqLiteDatabase.rawQuery("select music_info._id,music_info.musicName,music_info.album,music_info.artist,music_info.displayName,music_info.publish,music_info.duration,music_info.size,music_info.data from music_info left join" + listTableName + "on music_info._id=" + listTableName + "._id", null);
        if(cursor!=null&&cursor.moveToFirst()) {
            do{
                MusicBean music = new MusicBean();
                music.musicId = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
                music.musicName = cursor.getString(cursor.getColumnIndexOrThrow("musicName"));
                music.album = cursor.getString(cursor.getColumnIndexOrThrow("album"));
                music.artist = cursor.getString(cursor.getColumnIndexOrThrow("artist"));
                music.displayName = cursor.getString(cursor.getColumnIndexOrThrow("displayName"));
                music.publish = cursor.getString(cursor.getColumnIndexOrThrow("publish"));
                music.duration = cursor.getInt(cursor.getColumnIndexOrThrow("duration"));
                music.size = cursor.getInt(cursor.getColumnIndexOrThrow("size"));
                music.data = cursor.getString(cursor.getColumnIndexOrThrow("data"));
                if (music.duration >= 1 * 1000 * 60 && music.size > 1 * 1024 * 1024) {
                    musicList.add(music);
                }
            }while (cursor.moveToNext());
        }
        return musicList;
    }

    public ArrayList<MusicBean> getAllMusic(Context context) {
        ArrayList<MusicBean> mList = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = DatabaseHelper.getHelper(context).getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query(DatabaseHelper.TABLE_MUSIC, null, null, null, null, null, null);
        if(cursor!=null&&cursor.moveToFirst()) {
            do {
                MusicBean musicBean = new MusicBean();
                musicBean.musicId = cursor.getInt(cursor.getColumnIndexOrThrow("musicId"));
                musicBean.data = cursor.getString(cursor.getColumnIndexOrThrow("data"));
                musicBean.artist = cursor.getString(cursor.getColumnIndexOrThrow("artist"));
                musicBean.album = cursor.getString(cursor.getColumnIndexOrThrow("album"));
                musicBean.musicName = cursor.getString(cursor.getColumnIndexOrThrow("musicName"));
                musicBean.duration = cursor.getInt(cursor.getColumnIndexOrThrow("duration"));
                musicBean.size = cursor.getInt(cursor.getColumnIndexOrThrow("size"));
                musicBean.publish = cursor.getString(cursor.getColumnIndexOrThrow("year"));
                if (musicBean.duration >= 1 * 1000 * 60 && musicBean.size > 1 * 1024 * 1024) {
                    mList.add(musicBean);
                }
            }while (cursor.moveToNext());
        }
        cursor.close();
        return mList;
    }

    /*
    * 根据列表ID获取列表实例
    * */
    public ArrayList<MusicBean> getMusicList(Context context, int listId) {
        if (listId >= 0) {
            ArrayList<MusicBean> list = new ArrayList<>();
            SQLiteDatabase sqLiteDatabase = DatabaseHelper.getHelper(context).getWritableDatabase();
            /*Cursor cursor = sqLiteDatabase.rawQuery("select musicId from " + DatabaseHelper.TABLE_MUSIC_LIST_RELATIONSHIP + " where listId =" + listId, null);
            String[] selectArgs = new String[cursor.getCount()];
            int i = 0;
            while (cursor.moveToNext()) {
                selectArgs[i] = cursor.getInt(cursor.getColumnIndexOrThrow("musicId")) + "";
                i++;
            }*/
//            Cursor cursor=sqLiteDatabase.
            Cursor cursor1 = sqLiteDatabase.rawQuery("select * from " + DatabaseHelper.TABLE_MUSIC + " where musicId in ( select musicId from " + DatabaseHelper.TABLE_MUSIC_LIST_RELATIONSHIP
                    + " where listId=" + listId + ") ", null);
//            Cursor cursor1 = sqLiteDatabase.query(DatabaseHelper.TABLE_MUSIC,null,null,null, null, null, null);
            if (cursor1 != null && cursor1.moveToFirst()) {
                do {
                    MusicBean music = new MusicBean();
                    music.musicId = cursor1.getInt(cursor1.getColumnIndexOrThrow("musicId"));
                    music.musicName = cursor1.getString(cursor1.getColumnIndexOrThrow("musicName"));
                    music.album = cursor1.getString(cursor1.getColumnIndexOrThrow("album"));
                    music.artist = cursor1.getString(cursor1.getColumnIndexOrThrow("artist"));
                    music.publish = cursor1.getString(cursor1.getColumnIndexOrThrow("year"));
                    music.duration = cursor1.getInt(cursor1.getColumnIndexOrThrow("duration"));
                    music.size = cursor1.getInt(cursor1.getColumnIndexOrThrow("size"));
                    music.data = cursor1.getString(cursor1.getColumnIndexOrThrow("data"));
                    music.category = cursor1.getInt(cursor1.getColumnIndexOrThrow("category"));
                    music.filetype = cursor1.getInt(cursor1.getColumnIndexOrThrow("filetype"));
                    if (music.duration >= 1 * 1000 * 60 && music.size > 1 * 1024 * 1024) {
                        list.add(music);
                    }
                } while (cursor1.moveToNext());
            }
//            cursor.close();
            cursor1.close();
            return list;
        } else {
            return getAllMusic(context);
        }
    }


    /**
     * 功能 通过album_id查找 album_art 如果找不到返回null
     *
     * @param album_id
     * @return album_art
     */

    public String getAlbumArt(Context context, int album_id) {
        String mUriAlbums = "content://media/external/audio/albums";
        String[] projection = new String[]{"album_art"};
        Cursor cur = context.getContentResolver().query(
                Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)),
                projection, null, null, null);
        String album_art = null;
        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            album_art = cur.getString(0);
        }
        cur.close();
        if (album_art == null) {
            return null;
        }
        return album_art;
    }

    public void scanMusic(Context context) {
        SQLiteDatabase sqLiteDatabase = DatabaseHelper.getHelper(context).getWritableDatabase();
        Cursor cursor = MediaStoreAccessHelper.getAllSongUri(context, null);
        if(cursor!=null&&cursor.moveToFirst()) {
           do{
                ContentValues values = new ContentValues();
                values.put("data", cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                values.put("artist", cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
                values.put("album", cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
                values.put("musicName", cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
                values.put("duration", cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
                values.put("size", cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));
                values.put("year", cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.YEAR)));
                sqLiteDatabase.insert(DatabaseHelper.TABLE_MUSIC, null, values);
            } while (cursor.moveToNext());
        }
        cursor.close();
        sqLiteDatabase.close();
    }
}
